package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.MessageModel;
import com.hangzhou.xyj.model.type.EMessageType;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.multiimageselector.utils.TimeUtils;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaohuix on 2016/1/11.
 */
public class MyMessageAdapterV2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater inflater;
    private List<MessageModel> mDatas;
    private OnRecyclerViewItemClickListener mListener;

    public MyMessageAdapterV2(Context context, List<MessageModel> datas) {
        this.mContext = context;
        if (datas == null) {
            datas = new ArrayList<>();
        }
        this.mDatas = datas;
        inflater = LayoutInflater.from(context);
    }

    public void update(List<MessageModel> data) {
        if (data == null) {
            return;
        }
        //排下序：系统消息，个人消息，群消息
        List<MessageModel> tempData = new ArrayList<>();
        for (MessageModel model : data) {
            EMessageType type = EMessageType.valueOf(model.getType());
            if (type == EMessageType.SYSTEM) {
                tempData.add(model);
            }
        }

        for (MessageModel model : data) {
            EMessageType type = EMessageType.valueOf(model.getType());
            if (type == EMessageType.PERSON) {
                tempData.add(model);
            }
        }

        for (MessageModel model : data) {
            EMessageType type = EMessageType.valueOf(model.getType());
            if (type == EMessageType.GROUP) {
                tempData.add(model);
            }
        }

        this.mDatas = tempData;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getType();
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener) {
        mListener = listener;
    }

    /**
     * 绑定Header下面数据的ViewHolder
     *
     * @param viewHolder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder == null) {
            return;
        }
        final MessageItemViewHolder mHolder = (MessageItemViewHolder) viewHolder;
        mHolder.itemView.findViewById(R.id.smContentView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(v, mHolder.getItemViewType(), position);
            }
        });
        //点击删除的操作
        mHolder.itemView.findViewById(R.id.smMenuView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new TipsDialog(mContext, "提示", "确定删除？", "确定", "取消") {
                    @Override
                    protected void OnClickNegativeButton() {
                        this.dismiss();
                    }

                    @Override
                    protected boolean OnClickPositiveButton() {
                        mListener.onDeleteViewClick(v, position, mDatas);
                        return true;
                    }

                }.show();
            }
        });
        MessageModel message = mDatas.get(position);

        //前面图标处理
        EMessageType messageType = EMessageType.valueOf(getItemViewType(position));
        switch (messageType) {
            case SYSTEM:
                mHolder.mImageView.setImageResource(R.drawable.ic_notice);
                break;
            default:
                mHolder.mImageView.displayImage(message.getImage());
                break;
        }

        //名称:title
        switch (messageType) {
            case SYSTEM:
                mHolder.mTvName.setText("通知");
                break;
            default:
                mHolder.mTvName.setText(message.getTitle());
                break;
        }


        mHolder.mTvMessage.setText(message.getDesc());
        mHolder.mTvTime.setText(TimeUtils.timeFormat(message.getInsertTime(), "MM-dd HH:mm:ss"));
//        EMessageStatus status = EMessageStatus.valueOf(message.getStatus());
//        switch (status) {
//            case MESSAGE_STATUS_UNREAD: //未读
//                mHolder.mTvUnReadCount.setVisibility(View.VISIBLE);
//                break;
//            case MESSAGE_STATUS_READ: //已读
//                mHolder.mTvUnReadCount.setVisibility(View.GONE);
//                break;
//        }
        int unReadCount = message.getUnReadCount();
        if (unReadCount <= 0) {
            mHolder.mTvUnReadCount.setVisibility(View.GONE);
        } else if (unReadCount < 9) {
            mHolder.mTvUnReadCount.setVisibility(View.VISIBLE);
            mHolder.mTvUnReadCount.setText(unReadCount + "");
        } else { //大于9个显示9+
            mHolder.mTvUnReadCount.setVisibility(View.VISIBLE);
            mHolder.mTvUnReadCount.setText("9+");
        }
    }

    /**
     * 创建Header下面数据的ViewHolder
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int viewType) {
        View view = inflater.inflate(R.layout.layout_message_item, viewGroup, false);
        return new MessageItemViewHolder(view);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int viewType, int position);

        void onDeleteViewClick(View view, int position, List<MessageModel> messageList);
    }

    /**
     * 消息的ViewHolder类
     */
    class MessageItemViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mImageView;//头像Image
        MyTextView mTvName;//昵称
        MyTextView mTvMessage;//消息
        MyTextView mTvTime;//时间
        MyTextView mTvUnReadCount;//未读数

        public MessageItemViewHolder(View itemView) {
            super(itemView);
            mImageView = (CircleImageView) itemView.findViewById(R.id.miv_message_header);
            mTvName = (MyTextView) itemView.findViewById(R.id.mtv_message_name);
            mTvMessage = (MyTextView) itemView.findViewById(R.id.mtv_message_message);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_message_time);
            mTvUnReadCount = (MyTextView) itemView.findViewById(R.id.tv_un_read_count);
        }
    }

    /**
     * Header部分的ViewHolder
     */
    class HeaderItemViewHolder extends RecyclerView.ViewHolder {
        MyTextView mTvMore;
        MyTextView mTvGroupName;

        public HeaderItemViewHolder(View itemView) {
            super(itemView);
            mTvGroupName = (MyTextView) itemView.findViewById(R.id.tv_header_group);
            mTvMore = (MyTextView) itemView.findViewById(R.id.tv_more);
            //隐藏”更多“
            mTvMore.setVisibility(View.GONE);
        }
    }
}
